package com.jishuo.nontax.web.bill;

import com.jishuo.base.annotation.UserAuthority;
import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.bean.GridResult;
import com.jishuo.base.service.UserService;
import com.jishuo.nontax.bean.message.efisc.EpayFiscVO;
import com.jishuo.nontax.bfc.service.EpayFiscService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 缴款书
 */
@Slf4j
@Controller
@RequestMapping(value = "/bill/epayfisc", name = "电子缴库页面")
@UserAuthority(menu = {"bill/epayfisc","sys/basedata/basedata"})
public class EPayFiscController {

    private String prefix = "bill/";

    @Resource
    private EpayFiscService epayFiscService;
    @Resource
    private UserService userService;

    @RequestMapping(value = {"epayfisc", "epayfisc/{page}"}, name = "电子缴库页面")
    public String index(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "epayfisc" : page);
    }

    @ResponseBody
    @RequestMapping(value = "/selepayfisc")
    public GridResult findFisc(EpayFiscVO epayFiscVO) {
        return new GridResult<>(epayFiscService.findEpayFisc(epayFiscVO.getBillNum(), epayFiscVO.getPayBankDt(), epayFiscVO.getAccountCode()));
    }

    /**
     * 按划解国库单据发送
     * @param billid
     */
    @ResponseBody
    @RequestMapping(value = "/sendefisc")
    public BaseResult sendFisc(String billid,String usercode,String userpwd) {
            if(userService.isFHUser(userpwd, usercode)  > 0) {
                if(billid!=null && !billid.trim().equals("")){
                    String[] ids = billid.split(",");
                    for (int i=0;i<ids.length;i++) {
                        epayFiscService.sendFisc(ids[i], null);
                    }
                    return new BaseResult();
                }else{
                    return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "缺失参数");
                }
            }else{
                return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "用户信息不存在");
            }

    }

    /**
     * 发送划款流水
     * @param billid
     */
    @ResponseBody
    @RequestMapping(value = "/sendpayment")
    public BaseResult sendPayment(String billid,String usercode,String userpwd){
//        try {
            if(userService.isFHUser(usercode, userpwd) > 0) {
                if(billid!=null && !billid.trim().equals("")){
                    String[] ids = billid.split(",");
                    for (int i=0;i<ids.length;i++) {
                        epayFiscService.setBankPayment(ids[i], null);
                    }
                    return new BaseResult();
                }else{
                    return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "缺失参数");
                }
            }else{
                return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "用户信息不存在");
            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "请刷新页面重试，或联系管理员");
//        }
    }

    /**
     * 发送扣款流水
     * @param billid
     */
    @ResponseBody
    @RequestMapping(value = "/sendepayFisc", method = {RequestMethod.GET,RequestMethod.POST})
    public BaseResult sendEPayFisc(String billid,String usercode,String userpwd) {
//        try {
            if(userService.isFHUser(usercode, userpwd) > 0) {
                if(billid!=null && !billid.trim().equals("")){
                    String[] ids = billid.split(",");
                    for (int i=0;i<ids.length;i++) {
                        epayFiscService.setEPayFisc(ids[i]);
                    }
                    return new BaseResult();
                }else{
                    return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "缺失参数");
                }
            }else{
                return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "用户信息不存在");
            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), "请刷新页面重试，或联系管理员");
//        }
    }

}
